<script lang="ts" setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { qqLogin } from '@/api/user'
import { mobileRules, codeRules } from '@/utils/rules'
import { useCode } from '@/hook'
import { bindMobile } from '@/api/user'
import type { User } from '@/types/user'
import { useUserStore } from '@/stores'
import { useRoute, useRouter } from 'vue-router'
const route = useRoute()
const router = useRouter()
const store = useUserStore()
let _openId = ''
const isNeedBind = ref(false)
const mobile = ref('')
const code = ref('')
const { form, timer, time, send } = useCode(mobile, 'bindMobile')
onMounted(() => {
  if (window.QC.Login.check()) {
    // QQ登录成功了
    // openId就是刚刚扫码登录的QQ的唯一标识符
    window.QC.Login.getMe((openId: string) => {
      // 保存起来，方便后续绑定手机号使用
      _openId = openId
      qqLogin(openId)
        .then(res => {
          // todo 保存用户信息，跳转到用户页面
          loginSuccess(res.data)
        })
        .catch(err => {
          isNeedBind.value = true
          console.log('---err---', err)
        })
    })
  }
})
onUnmounted(() => {
  clearInterval(timer)
})
// 绑定手机号
const bind = async () => {
  const res = await bindMobile({
    mobile: mobile.value,
    code: code.value,
    openId: _openId
  })

  loginSuccess(res.data)
}
// 登录成功之后的处理
const loginSuccess = (user: User) => {
  // 保存用户信息到仓库中
  store.setUser(user)

  // 跳转到
  if (route.query.redirect) {
    // 跳转到重定向地址
    router.replace(route.query.redirect as string)
  } else {
    // 默认跳转到 user
    router.push('/user')
  }
}
</script>

<template>
  <div class="login-page" v-if="isNeedBind">
    <cp-nav-bar></cp-nav-bar>
    <div class="login-head">
      <h3>手机绑定</h3>
    </div>
    <van-form autocomplete="off" ref="form" @submit="bind">
      <van-field
        name="mobile"
        placeholder="请输入手机号"
        v-model="mobile"
        :rules="mobileRules"
      ></van-field>
      <van-field name="code" placeholder="请输入验证码" v-model="code" :rules="codeRules">
        <template #button>
          <span class="btn-send" :class="{ active: time > 0 }" @click="send">{{
            time > 0 ? `${time}秒之后再试` : '发送验证码'
          }}</span>
        </template>
      </van-field>
      <div class="cp-cell">
        <van-button block round type="primary" native-type="submit"> 立即绑定 </van-button>
      </div>
    </van-form>
  </div>
</template>
<style lang="scss" scoped>
.login {
  &-head {
    padding: 30px 30px 50px;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  &-other {
    margin-top: 60px;
    padding: 0 30px;
    .icon {
      display: flex;
      justify-content: center;
      img {
        width: 36px;
        height: 36px;
        padding: 4px;
      }
    }
  }
}
.van-form {
  padding: 0 14px;
  .cp-icon {
    margin-right: 10px;
  }
  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }
  .btn-send {
    color: var(--cp-primary);
    &.active {
      color: rgba(22, 194, 163, 0.5);
    }
  }
}
</style>
